package com.example.yk2021xm04.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.yk2021xm04.pojo.Meeting;
import com.example.yk2021xm04.mapper.MeetingMapper;
import com.example.yk2021xm04.query.MeetingQuery;
import com.example.yk2021xm04.service.IMeetingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.yk2021xm04.utils.AssertUtil;
import com.example.yk2021xm04.utils.PageResultUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZengQi
 * @since 2021-12-08
 */
@Service
public class MeetingServiceImpl extends ServiceImpl<MeetingMapper, Meeting> implements IMeetingService {
    @Override
    public void addMeeting(Meeting meeting) {
        /**
         * 通知内容
         * 非空
         */
        AssertUtil.isTrue(StringUtils.isBlank(meeting.getMeetingName()),"会议标题不能为空");
        AssertUtil.isTrue(null!=this.findMeetingByMeetingName(meeting.getMeetingName()),"会议名已存在");
        meeting.setCreateTime(LocalDateTime.now());
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        AssertUtil.isTrue(!(this.save(meeting)),"会议记录添加失败");
    }

    @Override
    public Map<String, Object> meetingList(MeetingQuery meetingQuery) {
        //分页的类
        IPage<Meeting> page = new Page<Meeting>(meetingQuery.getPage(), meetingQuery.getLimit());
        //详细可搜索相关用法
        QueryWrapper<Meeting> queryWrapper = new QueryWrapper<Meeting>();

        //这里就是代表 sql语句中的 like
        if(StringUtils.isNotBlank(meetingQuery.getMeetingName())) {
            queryWrapper.like("meeting_name",meetingQuery.getMeetingName());
        }
        //已经写好的接口
        page = this.baseMapper.selectPage(page,queryWrapper);
        System.out.println(page.getTotal());
        return PageResultUtil.getResult(page.getTotal(),page.getRecords());
    }

    @Override
    public void deleteMeeting(Integer[] ids) {
        AssertUtil.isTrue(null==ids || ids.length==0,"请选择待删除的id");
        AssertUtil.isTrue(!(this.removeByIds(Arrays.asList(ids))),"会议记录删除失败");
    }

    @Override
    public void updateMeeting(Meeting meeting) {
        AssertUtil.isTrue(StringUtils.isBlank(meeting.getMeetingName()),"会议标题不能为空");
        Meeting temp = this.findMeetingByMeetingName(meeting.getMeetingName());
        AssertUtil.isTrue(null!=temp&&!(temp.getId().equals(meeting.getId())),"会议名已存在");
        AssertUtil.isTrue(!(this.updateById(meeting)),"会议标题更新失败");
    }

    private Meeting findMeetingByMeetingName(String meetingContent) {
        return this.baseMapper.selectOne(new QueryWrapper<Meeting>().eq("meeting_name",meetingContent));
    }
}
